//
//  JourneyDetilViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyDetilViewController.h"
#import "JourneryNetHandler.h"
#import "JourneyDetilDataSource_Detil.h"
#import "UIImageView+WebCache.h"
#import "JourneyPhotoViewController.h"
#import "DetilCostumView.h"
#import "MBProgressHUD.h"

static CGFloat ImageHeight  = 200;
static CGFloat ImageWidth  = 320.0;

@interface JourneyDetilViewController ()<UIScrollViewDelegate, MBProgressHUDDelegate>

@property (nonatomic , retain)UIImageView *imgProfile;
@property (nonatomic , retain)NSMutableArray *resultArr;
@property (nonatomic , retain)UILabel *nameLable;
@property (nonatomic , retain)UILabel *recommended_resonLable;
@property (nonatomic , retain)DetilCostumView *baseInfo;
@property (nonatomic , retain)DetilCostumView *addressInfo;
@property (nonatomic , retain)DetilCostumView * arriveInfo;
@property (nonatomic , retain)DetilCostumView *openTimeInfo;
@property (nonatomic , retain)DetilCostumView *contectInfo;
@property (nonatomic , retain)MBProgressHUD *progress;
@end

@implementation JourneyDetilViewController
#pragma mark data
- (void)handlerNet

{
    NSLog(@"3333333");
    NSString *str = [[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.journeyData.type,self.journeyData.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"!!!!!!!%@",str);
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        self.resultArr = [data objectForKey:@"hottest_places"];
        NSString *str = [[_resultArr objectAtIndex:0] valueForKey:@"photo"];
        
        [self.imgProfile setImageWithURL:[NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        
        self.nameLable.text = [data objectForKey:@"name"];
        self.recommended_resonLable.text = [data objectForKey:@"recommended_reason"];
        self.baseInfo.myLable.text = [@"概述:" stringByAppendingString:[data objectForKey : @"description"]];
        self.addressInfo.myLable.text = [@"地址:" stringByAppendingString:[data objectForKey:@"address"]];
        self.arriveInfo.myLable.text = [@"到达方式:" stringByAppendingString:[data objectForKey:@"arrival_type"]];
        
        NSString *openStr = @"开放时间:";
        self.openTimeInfo.myLable.text = [openStr stringByAppendingString:[data objectForKey:@"opening_time" ]];
        
        
        NSString *contectStr = @"联系方式:";
        self.contectInfo.myLable.text  =[contectStr stringByAppendingString:[data objectForKey: @"tel" ]];
        
        
        [_progress hide:YES];
    }];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //     NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    CGFloat yOffset   = self.scrollView.contentOffset.y;
    CGFloat xOffset   = self.wrapperScroll.contentOffset.x;
    
    if (yOffset < 0) {
        
        CGFloat pageWidth = self.wrapperScroll.frame.size.width;
        int page = floor((self.wrapperScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        
        CGFloat factor = ((ABS(yOffset)+ ImageHeight )*ImageWidth)/ ImageHeight;
        
        CGRect f = CGRectMake((-(factor-ImageWidth)/2) + 320 *page, 20, ImageWidth, ImageHeight);
        f.size.height = ImageHeight+ABS(yOffset);
        f.size.width = factor;
        _imgProfile.frame = f;
        
        CGRect frame = self.imgContainer.frame;
        frame.origin.y = 0;
        self.imgContainer.frame = frame;
        
    } else {
        self.imgContainer.contentOffset = CGPointMake(xOffset, yOffset);
    }
    
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handlerNet];
    [self addView];
    
}

#pragma mark 加载视图
- (void)addView
{
    self.imgContainer  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ImageWidth, self.view.bounds.size.height)];
    self.imgContainer.backgroundColor = [UIColor clearColor];
    self.imgContainer.contentSize = CGSizeMake(ImageWidth, self.view.bounds.size.height);
    
    [self.view addSubview:self.imgContainer];
    self.imgProfile = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _imgContainer.frame.size.width, ImageHeight)];
    [self.imgContainer addSubview:self.imgProfile];
    
    UIView *detilView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height)];
    detilView.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    detilView.layer.cornerRadius = 10;
    detilView.clipsToBounds = YES;
    //    CGRect frame = detilView.frame;
    //    frame.origin.y = ImageHeight;
    //    detilView.frame = frame;
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = CGSizeMake(320, detilView.frame.size.height+ImageHeight);
    //    [self.scrollView addSubview:self.wrapperScroll];
    [self.scrollView addSubview:detilView];
    [self.view addSubview:self.scrollView];
    
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(detilView.frame.size.width / 3, 20, detilView.frame.size.width / 4, 50)];
    //    _nameLable.backgroundColor = [UIColor redColor];
    _nameLable.textAlignment = NSTextAlignmentCenter;
    [detilView addSubview:_nameLable];
    
    
    
    self.recommended_resonLable = [[UILabel alloc]initWithFrame:CGRectMake(0,90, detilView.frame.size.width, 30)];
    _recommended_resonLable.textAlignment = NSTextAlignmentCenter;
    _recommended_resonLable.font = [UIFont fontWithName:@"ChalkboardSE-Light" size:13];
    //    _recommended_resonLable.backgroundColor = [UIColor redColor];
    [detilView addSubview:_recommended_resonLable];
    
#pragma mark 添加菊花
    //添加菊花
    self.progress = [[MBProgressHUD showHUDAddedTo:self.view animated:YES]retain];
    [_progress show:YES];
    self.progress.delegate = self;
    
    
    //    UIButton *ratingBtu = [UIButton buttonWithTitle:@"添加点评" frame:btuFram target:self action:@selector(ratingBtuAction:)];
    //    ratingBtu.layer.cornerRadius = 10;
    //    ratingBtu.backgroundColor = [UIColor redColor];
    //
    //    [detilView addSubview:ratingBtu];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(detilView.frame.size.width / 3, _recommended_resonLable.frame.origin.y + 25, detilView.frame.size.width / 4, 30)];
    lable.text = @"基本信息";
    [detilView addSubview:lable];
    
    CGRect baseFram = CGRectMake(0, lable.frame.origin.y + 30, detilView.frame.size.width , 30);
    CGRect adressFram = CGRectMake(0, lable.frame.origin.y + 70, detilView.frame.size.width , 30);
    CGRect arriveFram = CGRectMake(0, lable.frame.origin.y + 110, detilView.frame.size.width , 30);
    CGRect openTimeFram = CGRectMake(0, lable.frame.origin.y + 150, detilView.frame.size.width , 30);
    CGRect contectFram = CGRectMake(0, lable.frame.origin.y + 190, detilView.frame.size.width , 30);
    
    self.baseInfo = [[DetilCostumView alloc]initWithFrame:baseFram];
    [detilView addSubview:_baseInfo];
    
    self.addressInfo = [[DetilCostumView alloc]initWithFrame:adressFram];
    [detilView addSubview:_addressInfo];
    
    self.arriveInfo = [[DetilCostumView alloc]initWithFrame:arriveFram];
    [detilView addSubview:_arriveInfo];
    
    self.openTimeInfo = [[DetilCostumView alloc]initWithFrame:openTimeFram];
    [detilView addSubview:_openTimeInfo];
    
    self.contectInfo = [[DetilCostumView alloc]initWithFrame:contectFram];
    [detilView addSubview:_contectInfo];
    
    CGRect backFram =CGRectMake(0, 5, 50, 50);
    UIButton *backBtu = [UIButton buttonWithTitle:@"<" frame:backFram target:self action:@selector(backBtuAction:)];
    backBtu.backgroundColor = [UIColor whiteColor];
    backBtu.layer.cornerRadius = 25;
    backBtu.alpha = 0.5;
    [self.view addSubview:backBtu];
    
}

#pragma  mark MoreBtu
- (void)ratingBtuAction : (UIButton *)btu
{
    NSLog(@"111");
}

#pragma mark backBtu
- (void)backBtuAction: (UIButton *)btu
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 结束拖拽的时候推出新的页面

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y == 0) {
        JourneyPhotoViewController *jourPhotoVC = [[JourneyPhotoViewController alloc]init];
        jourPhotoVC.idNumber = self.journeyData.idNumber;
        jourPhotoVC.type = self.journeyData.type;
        jourPhotoVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:jourPhotoVC animated:YES];
        
        [jourPhotoVC release];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = YES;
    CGRect bounds = self.view.frame;
    self.scrollView.frame = bounds;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
